package com.song.test.datautil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;

public class ConvertFile {
	public static void main(String[] args) {
		ConvertFile.convert("E:\\TDDOWNLOAD\\csdn泄露的600万明文帐号密码.sql", "E:\\csdn",5000);
	}
	
	public static void convert(String srcFile,String descFile,int max){
		FileReader fReader = null;
		BufferedReader bReader = null;
		FileWriter fWriter = null;
		BufferedWriter bWriter = null;
		try {
			fReader = new FileReader(new File(srcFile));
			bReader = new BufferedReader(fReader);
			
			fWriter = new FileWriter(new File("E:\\csdn\\csdn1" + ".txt"));
			bWriter = new BufferedWriter(fWriter);
			
			int count = 0;
			int i = 1;
			String temp = null;
			while((temp = bReader.readLine()) != null){
				if(count >= 5000){
					bWriter.close();
					fWriter = new FileWriter(new File("E:\\csdn\\csdn" + ++i + ".txt"));
					bWriter = new BufferedWriter(fWriter);
					count = 0;
				}
				if(StringUtils.isNotBlank(temp)){
					String[] split = temp.split("#");
					if(null != split && split.length > 2){
						bWriter.write(split[2].trim()+"\n");
						count++;
					}
				}
				temp = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(bReader != null){
				try {
					bReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != fReader){
				try {
					fReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null != bWriter){
				try {
					bWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fWriter != null){
				try {
					fWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
